package net.yxsoft.controller.pc.sysmgt.infrastructure;

import com.jfinal.aop.Before;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import javafx.scene.control.Pagination;
import net.yxsoft.bean.JsTreeNode;
import net.yxsoft.bean.JsTreeNodeFieldKey;
import net.yxsoft.bean.TsCodeSearch;
import net.yxsoft.enumclass.LevelEnumCls;
import net.yxsoft.enumclass.StateEnumCls;
import net.yxsoft.kit.JsTreeNodeKit;
import net.yxsoft.render.PoiRender;
import net.yxsoft.controller.pc.sysmgt.common.AuthController;
import net.yxsoft.controller.pc.sysmgt.common.validator.CodeValidator;
import net.yxsoft.model.infrastructure.TsCode;
import net.yxsoft.service.infrastructure.CodeService;
import net.yxsoft.bean.BSTablePage;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * Created by mlb on 2016/5/10.
 */
public class CodeController extends AuthController {

  static final CodeService srv = CodeService.INSTANCE;

  /**
  * @MethodName: index
  * @Description: 数据字典主入口
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:22
  * @author jhf
  */
  public void index() throws IllegalAccessException {

    //级别查询条件控件数据源
    setAttr("LevelEmunSource", LevelEnumCls.jsonText(LevelEnumCls.class, LevelEnumCls.COLLECTS_DEFAULTS));
    setAttr("LevelddlSource", LevelEnumCls.COLLECTS_DEFAULTS);
    render("code.html");
  }

  /**
  * @MethodName: getCode
  * @Description: 获取数据字典信息返回grid格式
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:22
  * @author jhf
  */
  public void getCode() {
    TsCodeSearch _searchModel = getBean(TsCodeSearch.class, "");
    BSTablePage bsTablePage = BSTablePage.init(this);

    Kv cond = Kv.create();
    if(_searchModel != null) {

      if(StrKit.notBlank(_searchModel.getParentId())) {

        cond.set("parentid = ", _searchModel.getParentId());
      }
      if(StrKit.notBlank(_searchModel.getCodeName())) {
        cond.set("codename like ", "%" + _searchModel.getCodeName() + "%");
      }
      String codename=getPara("codename");
      if(StrKit.notBlank(codename)){
        cond.set("codename like ", "%" + codename + "%");
      }
      if(StrKit.notBlank(_searchModel.getToParentCodeId())) {
        String parentId = Db.queryStr("select parentid from ts_code where codeid = ?", _searchModel.getToParentCodeId());
        cond.set("parentid = ", parentId);
      }
    }
    /*srv.paging(bsTablePage, "code.codePageSelect", Kv.by("cond", cond));

    renderJson(bsTablePage.getResult());*/
    renderJson(srv.query("code.codePageSelect",Kv.by("cond",cond), rapid.jfinal.Pagination.by(this)));
  }

  /**
  * @MethodName: exportCodes
  * @Description: 导出数据字典信息
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:22
  * @author jhf
  */
  public void exportCodes() {
    String _pid = this.getPara("parentId", ""); //父节点ID
    String _codeName = this.getPara("codeName", "");//节点名称

    List<TsCode> ls = srv.exportCodes(_pid, _codeName);
    String[] clumns = {"CODEID", "CODENAME", "PARENTID", "PARENTNAME", "CUSTOMID", "SPECIALID", "ENABLED_CN", "SORTNO"};
    String[] heades = {"编号", "名称", "上级编号", "上级名称", "自编号", "特殊编号", "状态", "排序"};
    render(PoiRender.me(ls).fileName("DataDictionary.xls").sheetName("数据字典").headers(heades).columns(clumns).cellWidth(5000).headerRow(1));
  }

  /***
  * @MethodName: goAddCode
  * @Description: 打开新增界面前期数据处理方法
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:22
  * @author jhf
  */
  @Before(POST.class)
  public void goAddCode() {

    String _pid = this.getPara("parentId", "");
    TsCode model1 = new TsCode();

    if(_pid != "" && !_pid.equals("0")) {
      TsCode model = new TsCode().findFirst("SELECT * FROM TS_CODE WHERE CODEID=?", _pid);
      model1.setParentid(model.getCodeid());
      model1.setParentname(model.getCodename());
      model1.setParentcustomid(model.getCustomid());
    } else {
      model1.setParentname("此项为顶级");
    }

    renderJson(model1);
  }

  /**
  * @MethodName: goUpdateCode
  * @Description: 打开修改界面，根据主键id获取被修改信息数据
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:22
  * @author jhf
  */
  @Before(POST.class)
  public void goUpdateCode() {
    TsCode model = new TsCode().findById(this.getPara("Id"));
    if(model.getParentid() == null || model.getParentid() == "" || model.getParentid() == "0" || "0".equalsIgnoreCase(model.getParentid())) {
      model.setParentname("此项为顶级");
    }
    renderJson(model);
  }

  /**
  * @MethodName: addCode
  * @Description: 新增数据字典信息
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:22
  * @author jhf
  */
  @Before({POST.class, CodeValidator.class})
  public void addCode() {
    TsCode _m = getModel(TsCode.class, "");
    String parentid=getPara("parentid");
    _m.setParentid(parentid);
    renderJson(srv.addCode(_m,getLoginUser()));
  }

  /**
  * @MethodName: updateCode
  * @Description: 修改数据字典信息
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:22
  * @author jhf
  */
  @Before({POST.class, CodeValidator.class})
  public void updateCode() {
    TsCode _m = getModel(TsCode.class, "");
    renderJson(srv.updateCode(_m,getLoginUser()));
  }

  /**
  * @MethodName: removeCodes
  * @Description: 删除数据字典信息
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:23
  * @author jhf
  */
  public void removeCodes() {
    String codeids = getPara("codeids");
    if(!StrKit.isBlank(codeids)) {
      srv.removeCodes(codeids.split(","),getLoginUser());
    }
    renderEmpty();
  }

  /**
  * @MethodName: changeState
  * @Description: 修改状态：启用，禁用
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:23
  * @author jhf
  */
  public void changeState() throws IllegalAccessException {
    String codeids = getPara("codeids");
    String statestr = getPara("state");
    int _result = 0;
    if(!StrKit.isBlank(codeids) && !StrKit.isBlank(statestr)) {

      StateEnumCls state = StateEnumCls.getObject(StateEnumCls.class, statestr);

      _result = srv.changeState(codeids.split(","), state,getLoginUser());
    }
    renderJson(_result);
  }

  /**
  * @MethodName: getjsTreeByCode
  * @Description: 获取界面左侧树形结构数据组
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:23
  * @author jhf
  */
  public void getjsTreeByCode() throws NoSuchMethodException, SecurityException, InvocationTargetException, IllegalAccessException {
    JsTreeNodeFieldKey tnfk = new JsTreeNodeFieldKey("Codeid", "Codename", "Parentid");
    tnfk.setLeafKey("Leaf");
    tnfk.setLeafValue("0");

    List<TsCode> codelist = TsCode.dao.find("SELECT * FROM TS_CODE ORDER BY SORTNO");

    List<JsTreeNode> codeTreelist = JsTreeNodeKit.SerializeJsTreeData(tnfk, codelist);

    renderJson(codeTreelist);

  }

  /**
  * @MethodName: getCodeTopInfo
  * @Description: 模型获取 关联代码
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:23
  * @author jhf
  */
  public void getCodeTopInfo() {
    renderJson(srv.getCodeTopInfo());
  }

}
